package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.Sequence;
import org.csu.mypetstore.persistence.SequenceDao;
import org.csu.mypetstore.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;


public class SequenceDaoImpl implements SequenceDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public Sequence getSequence(Sequence sequence) {
        String sql = " SELECT name, nextid\n" +
                "    FROM SEQUENCE\n" +
                "    WHERE NAME = ?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Sequence>(Sequence.class),sequence.getName());
    }

    @Override
    public void updateSequence(Sequence sequence) {
        String sql = " UPDATE SEQUENCE\n" +
                "    SET NEXTID = ?\n" +
                "    WHERE NAME = ?";
        template.update(sql,sequence.getNextId(),sequence.getName());
    }
}
